import http from "@/utils/request";

// 获取所有设备信息

export const getDeviceListApi = async () => {
  try {
    const res = await http.get("/device/info/get/all");
    if (res && res.code === 200 && res.data) {
      const list = JSON.parse(res.data);
      return list;
    }
    return [];
  } catch (error) {
    console.log(error);
    return [];
  }
};

// 更新设备信息
export const updateDeviceApi = async (data) => {
  try {
    await http.post("/device/info/update", data);
    return true;
  } catch (error) {
    console.log(error);
    return false;
  }
};

// 添加设备
export const addDeviceApi = async (data) => {
  try {
    await http.post("/device/add", data);
    return true;
  } catch (error) {
    console.log(error);
    return false;
  }
};

// 删除设备
export const deleteDeviceApi = async (data) => {
  try {
    await http.post("/device/delete", { ...data });
    return true;
  } catch (error) {
    console.log(error);
    return false;
  }
};

// 获取单台设备信息
export const getDeviceDetailApi = async (deviceId) => {
  try {
    const res = await http.get(`/device/info/get?deviceId=${deviceId}`);
    if (res && res.code === 200 && res.data) {
      const detail = JSON.parse(res.data);
      return detail;
    }
    return {};
  } catch (error) {
    console.log(error);
    return {};
  }
};

// 获取单台设备故障信息
export const getDeviceFaultApi = async (deviceId) => {
  try {
    const res = await http.get(`/fault/info/get?deviceId=${deviceId}`);
    if (res && res.code === 200 && res.data) {
      const fault = JSON.parse(res.data);
      return fault;
    }
    return [];
  } catch (error) {
    console.log(error);
    return [];
  }
};

// 获取所有设备故障信息
export const getFaultListApi = async () => {
  try {
    const res = await http.get("/fault/info/get/all");
    if (res && res.code === 200 && res.data) {
      const list = JSON.parse(res.data);
      return list;
    }
    return [];
  } catch (error) {
    console.log(error);
    return [];
  }
};

// 获取压力信息
export const getPressureApi = async (deviceId) => {
  try {
    const res = await http.get(`/pressure/info/get?deviceId=${deviceId}`);
    if (res && res.code === 200 && res.data) {
      const list = JSON.parse(res.data);
      return list;
    }
    return [];
  } catch (error) {
    console.log(error);
    return [];
  }
};

// 获取温度信息
export const getTemperatureApi = async (deviceId) => {
  try {
    const res = await http.get(`/temperature/info/get?deviceId=${deviceId}`);
    if (res && res.code === 200 && res.data) {
      const list = JSON.parse(res.data);
      return list;
    }
    return [];
  } catch (error) {
    console.log(error);
    return [];
  }
};

// 获取单台设备状态信息
export const getDeviceStatusApi = async (deviceId) => {
  try {
    const res = await http.get(`/device/status/info/get?deviceId=${deviceId}`);
    if (res && res.code === 200 && res.data) {
      const status = JSON.parse(res.data);
      return status;
    }
    return {};
  } catch (error) {
    console.log(error);
    return {};
  }
};

// 获取所有设备状态信息
export const getDeviceStatusListApi = async () => {
  try {
    const res = await http.get("/device/info/get/all");
    console.log(res, 111);
    if (res && res.code === 200 && res.data) {
      const list = JSON.parse(res.data);
      return list;
    }
    return [];
  } catch (error) {
    console.log(error);
    return [];
  }
};
